<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;


/**
 * 拼团接口
 */
class TeamActivity extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\TeamActivity();

    }


    /**
     * 查看
     */
    public function index()
    {
        $this->limitFields = 'id,name,team_price,needer,goods_id,sales_sum,is_end,item_id';

        list($where, $sort, $order, $offset, $limit , $field) = $this->buildparams();



        $total = $this->model
            ->with('goods')
            ->where($where)
            ->where(function($query) use ($where){
                $query->where('status',1);
                $query->where('is_end',0);
                $query->where('start_time','<',time());
                $query->where('end_time','>',time());
            })
            ->order($sort, $order)
            ->count();

        $list = $this->model
            ->with('goods')
            ->field($field)
            ->where($where)
            ->where(function($query) use ($where){
                $query->where('status',1);
                $query->where('is_end',0);
                $query->where('start_time','<',time());
                $query->where('end_time','>',time());
            })
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();

        $result = array("total" => $total, "rows" => $list);

        $this->success('',$result);
    }

    /**
     * 详情
     */
    public function detail($goods_id = NULL)
    {
        \app\common\model\Goods::addzuji($goods_id,$this->auth->id);

        $row = \app\common\model\Goods::get($goods_id);
        if (!$row)
            $this->error(__('No Results were found'));

        $goodsAttrData = Db::name('goods_attr')->alias('ga')
            ->join('attr a','ga.attr_id = a.id','LEFT')
            ->where(['goods_id' => $goods_id])
            ->field('a.name')
            ->find();
        $row['attrName'] = $goodsAttrData['name'];

        $goodsAttrM = model('common/goodsAttr');
        $goodsAttr = $goodsAttrM->field('id,attr_value')->where(['goods_id' => $goods_id])->select();

        $row['goodsAttr'] = $goodsAttr;
        $row['type'] = \app\common\model\Goods::getType();


        if($row['prom_type'] == 2){
            $row['type']  = [
                ['id'=>'0','name'=>'自己买'],
                ['id'=>'1','name'=>'送一人']
            ];
        }

        $goodsPromFactory = new \app\common\logic\GoodsPromFactory();
        if (!empty($row['prom_id']) && $goodsPromFactory->checkPromType($row['prom_type'])) {
            $goodsPromLogic = $goodsPromFactory->makeModule($row);//这里会自动更新商品活动状态，所以商品需要重新查询
            $row = $goodsPromLogic->getGoodsInfo();//上面更新商品信息后需要查询
//            dump($goodsPromLogic);exit;
            $ActivityGoodsInfo = $goodsPromLogic->getActivityGoodsInfo();

//            dump($ActivityGoodsInfo->toArray());exit;
            if(isset($ActivityGoodsInfo['item_id']) && $ActivityGoodsInfo['item_id']){
                $ActivityGoodsInfo['goodsAttr'] = $goodsAttrM->field(['id,attr_value'])->where(['id' =>$ActivityGoodsInfo['item_id']])->select();
            }


            $row = $ActivityGoodsInfo;
        }




        $this->success('',$row);
    }








}